using SDK.Scripts.App;
using SDK.Scripts.Module;
using SDK.Scripts.Utils;
using SimpleJson;

namespace FrameWork.Scripts.Module
{
	public class GFW_Login : BaseModule
	{
		public delegate void LoginFunc(bool ret, UserInfo userInfo);

		private enum CallBackCode
		{
			BACK_FAILED,
			BACK_SUCCESS,
			BACK_CANCLE
		}

		private LoginFunc _loginBack;

		private UserInfo _userInfo;

		public GFW_Login()
		{
			_userInfo = default(UserInfo);
			GameSDK.GamePlat.gamePlatEvent += OnEventBack;
		}

		private void OnEventBack(string methonName, int code, string data)
		{
			if (!(methonName == "login"))
			{
				return;
			}
			bool flag = code == 1;
			if (flag)
			{
				if (ParseLoginMsg(data))
				{
					Debuger.LogDebug("登录成功:" + _userInfo.userId);
				}
				else
				{
					flag = false;
					Debuger.LogDebug("登录失败");
				}
			}
			else
			{
				flag = false;
				Debuger.LogDebug("登录失败:" + data);
			}
			if (_loginBack != null)
			{
				_loginBack(flag, _userInfo);
			}
		}

		protected bool ParseLoginMsg(string loginMsg)
		{
			JsonObject jsonObject = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(loginMsg);
			if (jsonObject != null)
			{
				int num = -1;
				if (jsonObject.ContainsKey("ret"))
				{
					num = Util.string2int(jsonObject["ret"].ToString());
				}
				if (num != 1)
				{
					Debuger.LogDebug("login plat faild ret = " + num);
					return false;
				}
				if (jsonObject.ContainsKey("userId"))
				{
					_userInfo.userId = Util.string2int(jsonObject["userId"].ToString());
				}
				if (jsonObject.ContainsKey("time"))
				{
					_userInfo.timeExpire = Util.string2long(jsonObject["time"].ToString());
				}
				if (jsonObject.ContainsKey("gender"))
				{
					_userInfo.gender = ((!(jsonObject["gender"].ToString() == "F")) ? 1 : 0);
				}
				if (jsonObject.ContainsKey("accountName"))
				{
					_userInfo.nickName = jsonObject["accountName"].ToString();
				}
				if (jsonObject.ContainsKey("icon"))
				{
					_userInfo.headIndex = Util.string2int(jsonObject["icon"].ToString());
				}
				if (jsonObject.ContainsKey("iconUrl"))
				{
					_userInfo.picUrl = jsonObject["iconUrl"].ToString();
				}
				if (jsonObject.ContainsKey("deviceToken"))
				{
					_userInfo.deviceToken = jsonObject["deviceToken"].ToString();
				}
				if (jsonObject.ContainsKey("platCoin"))
				{
					_userInfo.platCoin = Util.string2float(jsonObject["platCoin"].ToString());
				}
				_userInfo.loginInfo = jsonObject;
				return true;
			}
			return false;
		}

		public void Login(bool isGuest, LoginFunc cb = null)
		{
			_loginBack = cb;
			LoginWeb(isGuest);
		}

		private void LoginWeb(bool isGuest)
		{
			GameSDK.GamePlat.Login(isGuest);
		}
	}
}
